/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.controller.wx;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.ElevatorWxNumberStatistic;
import org.springblade.modules.admin.entity.MaintainPlanContent;
import org.springblade.modules.admin.enums.MaintainPlanContentEnum;
import org.springblade.modules.admin.enums.StatusEnum;
import org.springblade.modules.admin.enums.TaskStatusEnum;
import org.springblade.modules.admin.service.IElevatorWxNumberStatisticService;
import org.springblade.modules.admin.service.IMaintainPlanContentService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.admin.entity.ElevatorWxNumber;
import org.springblade.modules.admin.vo.ElevatorWxNumberVO;
import org.springblade.modules.admin.wrapper.ElevatorWxNumberWrapper;
import org.springblade.modules.admin.service.IElevatorWxNumberService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 电梯维修故障次数标准表 控制器
 *
 * @author ysq
 * @since 2024-07-05
 */
@RestController
@AllArgsConstructor
@RequestMapping("admin/elevator/wx/number")
@Api(value = "电梯维修故障次数标准表", tags = "电梯维修故障次数标准表接口")
public class ElevatorWxNumberController extends BladeController {

	private final IElevatorWxNumberService elevatorWxNumberService;

	private final IElevatorWxNumberStatisticService elevatorWxNumberStatisticService;

	private final IMaintainPlanContentService maintainPlanContentService;

	/**
	 * 分页 电梯维修故障次数标准表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入elevatorWxNumber")
	public R<List<ElevatorWxNumber>> list(String status) {
		if (Func.isNotEmpty(status)) {
			List<ElevatorWxNumber> list = elevatorWxNumberService.list(Wrappers.<ElevatorWxNumber>lambdaQuery().in(ElevatorWxNumber::getStatus, Func.toIntList(status)));
			return R.data(list);
		}
		List<ElevatorWxNumber> list = elevatorWxNumberService.list();
		return R.data(list);
	}


	/**
	 * 新增 电梯维修故障次数标准表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入elevatorWxNumber")
	public R save(@Valid @RequestBody ElevatorWxNumber elevatorWxNumber) {
		List<ElevatorWxNumber> list = elevatorWxNumberService.list(Wrappers.<ElevatorWxNumber>lambdaQuery().eq(ElevatorWxNumber::getDays, elevatorWxNumber.getDays()).eq(ElevatorWxNumber::getRepetitionCounts, elevatorWxNumber.getRepetitionCounts()).ne(ElevatorWxNumber::getStatus, 0));
		if (list.size() > 0) {
			throw new ServiceException("数据重复");
		}
		elevatorWxNumber.setTypeDesc("重复故障" + elevatorWxNumber.getDays() + "天/" + elevatorWxNumber.getRepetitionCounts() + "次");
		return R.status(elevatorWxNumberService.save(elevatorWxNumber));
	}

	/**
	 * 更新 电梯维修故障次数标准表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "更新", notes = "传入elevatorWxNumber")
	public R update(@Valid @RequestBody List<ElevatorWxNumber> list) {
		return R.status(elevatorWxNumberService.updateBatchById(list));
	}

	/**
	 * 分页 电梯维修故障次数标准表
	 */
	@GetMapping("/getElevatorWxNumberList")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "电梯重复故障列表", notes = "电梯重复故障列表")
	public R<List<ElevatorWxNumberStatistic>> getElevatorWxNumberList(Long elevatorId) {

		List<ElevatorWxNumberStatistic> list = new ArrayList<>(16);

		List<Integer> status = new ArrayList<>();
		status.add(1);
		status.add(2);
		List<ElevatorWxNumber> numbers = elevatorWxNumberService.list(Wrappers.<ElevatorWxNumber>lambdaQuery().in(ElevatorWxNumber::getStatus, status));
		if (numbers.size() == 0) {
			return R.data(new ArrayList<>());
		}
		List<Integer> days = numbers.stream().map(ElevatorWxNumber::getDays).distinct().collect(Collectors.toList());
		Integer max = days.stream().reduce(days.get(0), Integer::max) * -1;
		List<MaintainPlanContent> contentList = maintainPlanContentService.getPlanListOfElevatorId(elevatorId, DateUtil.offsetDay(new Date(), max), new Date());
		contentList = contentList.stream().filter(o -> o.getStatus() != TaskStatusEnum.WX_CLOSED.getStatus() && o.getStatus() != TaskStatusEnum.WX_TURN_DOWN.getStatus() && o.getStatus() != TaskStatusEnum.WX_CHECK_PASS_CLOSED.getStatus()).collect(Collectors.toList());
		for (ElevatorWxNumber elevatorWxNumber : numbers) {
			Date offsetDay = DateUtil.offsetDay(new Date(), elevatorWxNumber.getDays() * -1);
			Integer counts = 0;
			for (MaintainPlanContent maintainPlanContent : contentList) {
				if (maintainPlanContent.getCreateTime().after(offsetDay)) {
					counts = counts + 1;
				}
			}
			if (counts >= elevatorWxNumber.getRepetitionCounts()) {
				ElevatorWxNumberStatistic elevatorWxNumberStatistic = new ElevatorWxNumberStatistic();
				elevatorWxNumberStatistic.setElevatorId(elevatorId);
				elevatorWxNumberStatistic.setWxNumberId(elevatorWxNumber.getId());
				elevatorWxNumberStatistic.setWxNumberDesc(elevatorWxNumber.getTypeDesc());
				list.add(elevatorWxNumberStatistic);
			}
		}
		return R.data(list);
	}


	/**
	 * 删除 电梯维修故障次数标准表
	 */
	@GetMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(Long id) {
		List<ElevatorWxNumberStatistic> list = elevatorWxNumberStatisticService.list(Wrappers.<ElevatorWxNumberStatistic>lambdaQuery().eq(ElevatorWxNumberStatistic::getWxNumberId, id));
		if (list.size() > 0) {
			ElevatorWxNumber elevatorWxNumber = elevatorWxNumberService.getById(id);
			elevatorWxNumber.setStatus(StatusEnum.DISABLE.getId());
			elevatorWxNumberService.updateById(elevatorWxNumber);
		} else {
			elevatorWxNumberService.removeById(id);
		}
		return R.data(true);
	}


}
